import java.util.Scanner;

public class 最长公共子序列 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n1 = in.nextInt();
        int n2 = in.nextInt();
        String s1 = in.next();
        String s2 = in.next();
        //保持下标映射关系
        s1 = " " + s1;
        s2 = " " + s2;
        //字符串s1中0 - i个和字符串s2中0 - j个里面有几个最长公共子序列
        int[][] dp = new int[n1 + 1][n2 + 1];
        for(int i = 1;i <= n1;i++){
            for(int j = 1;j <= n2;j++){
                dp[i][j] = (s1.charAt(i) == s2.charAt(j)) ? dp[i - 1][j - 1] + 1 : Math.max(dp[i - 1][j],dp[i][j - 1]);
            }
        }

        System.out.println(dp[n1][n2]);
    }
}
